@echo off
rem uncomment 'd' defining for debug messages
rem set d=1

rem goto batch folder
cd /d %~dp0

set MSBuildTools=%windir%\Microsoft.NET\Framework\v3.5\
set MSBuild=%MSBuildTools%msbuild.exe
set SlnDir=%CD%\..\
set sln="%SlnDir%LightPersist.sln"
set ilmerge="%SlnDir%_ThirdParty\ilmerge"
set out=%SlnDir%Bin\
set deploy=Deploy\
set merge=LightPersist.Core.dll LightPersist.DomainGenerator.dll LightPersist.Storage.InmemoryStorage.dll LightPersist.Storage.XmlStorage.dll MSBuildAppDomainWrapper.dll
set mobile="LightPersist.Core Mobile.dll" LightPersist.DomainGenerator.dll "LightPersist.Storage.InmemoryStorage Mobile.dll" "LightPersist.Storage.XmlStorage Mobile.dll" MSBuildAppDomainWrapper.dll

call :CHECK_MSBUILD_PATH msbuild.exe
if errorlevel 1 goto :error
if defined d cd
if defined d echo OK.
if defined d echo Checking solution location...
if not exist %sln% call :ERROR_SLN_DIR
if defined d echo OK.

if exist %deploy% rd /s /q %deploy%
md %deploy%
call :build Debug
call :build Release

rem SUB FUNCTION
goto :eof
:BUILD
echo ==== Building %1 ====
msbuild %sln% /p:configuration=%1 /v:q
if errorlevel 1 goto :error
set to=%deploy%%1\Desktop\Separated\
md %to%
for %%I in (%merge%) do copy "%out%%1\%%~I" %to%
for %%I in (%merge%) do if exist "%out%%1\%%~nI.xml" copy "%out%%1\%%~nI.xml" %to%
echo Merging into single assembly
set cd_=%cd%
cd "%out%%1"
%ilmerge% /out:LightPersist.dll %merge%
if errorlevel 1 goto :error
cd %cd_%
set to=%deploy%%1\Desktop\Single\
md %to%
copy "%out%%1\LightPersist.dll" %to%
set to=%deploy%%1\Mobile\
md %to%
for %%I in (%mobile%) do copy "%out%%1\%%~I" %to%
echo DONE


rem SUB FUNCTION
goto :eof
:CHECK_MSBUILD_PATH
if defined d echo Checking msbuild in PATH...
if defined d if "%~f$PATH:1"=="" echo Add...
if "%~f$PATH:1"=="" path %path%;%MSBuildTools%
if defined d echo OK.

rem SUB FUNCTION
goto :eof
:error
echo FATAL ERROR!

rem SUB FUNCTION
goto :eof
:ERROR_SLN_DIR
echo Solution dirrectory not found
pause
%errorlevel%=1